/**
* @file response_get_list.cpp
*
*	 Nel file response_get_list.cpp viene implementata la classe Response_Get_List
*	
* @author Favale Angelo
* @author Fagioli Alessio
*
* @version 1.0
*/

#include "response_get_list.h"

Response_Get_List::Response_Get_List(host peer)
{

	strcpy(this->name, "Get_List");
	strcpy(this->entita.ip,peer.ip);
	this->entita.porta=peer.porta;
	this->entita.s=peer.s;

}


bool Response_Get_List::code_request()
{
	char buffer[BUFFER_SIZE];

	sprintf(buffer,"%s#",this->name);
	
	if (send(entita.s,buffer,strlen(buffer),0)==-1)
		{		
		printf("Errore invio\n");
		return false;
		}
	
	return true;



}


bool Response_Get_List::code_response(char * buffer)
{
	if (send(entita.s,buffer,strlen(buffer),0)==-1)
		{		
		printf("Errore invio\n");
		return false;
		}
	
	return true;

}


bool Response_Get_List::decode_response()
{

	char buffer[BUFFER_SIZE];
	int i=0;
	FILE *f;
	f=fopen("foto_server.txt","w");


	memset(buffer,0,BUFFER_SIZE);
		
	recv(entita.s,buffer,BUFFER_SIZE,0); //ricevo in una volta tutto il buffer con
							// la lista di file disponibili

	while(buffer[i]!='#' && i<BUFFER_SIZE)
	{
		while(buffer[i]!='/') 
			i++;
		i++;		
		while (buffer[i]!=';' && buffer[i]!='#')
			{			
			fprintf(f,"%c",buffer[i++]);
			printf("%c",buffer[i-1]);
			}
		if (buffer[i]==';')
				{fprintf(f,"\n");
				 printf("\n");				
				}
		else{
				fclose(f);
				break;
		    }
		
	}

	printf("\n");

	if(i==BUFFER_SIZE)
		return false;
	else
		return true;

}

///////////////////////////////////////////////////////////////////////
